package com.jiayang.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jiayang.base.system.employee.domain.bo.EmployeeBO;
import com.jiayang.base.system.login.domain.RequestTokenBO;
import com.jiayang.util.JYangRequestTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ Author     ：OuyangChenglong
 * @ Date       ：Created in 9:58 2021/9/6
 * @ Description：Mybatis-puls自动填充配置类
 * @ Modified By：
 * @Version: 1.0$
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增时自动填充逻辑
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        this.strictInsertFill(metaObject, "create_time", Date.class, new Date());
        RequestTokenBO threadLocalUser = JYangRequestTokenUtil.getThreadLocalUser();
        EmployeeBO employeeBO = threadLocalUser.getEmployeeBO();
        String loginName = employeeBO.getLoginName();
        this.strictInsertFill(metaObject, "create_man", String.class, loginName);

    }

    /**
     * 更新时自动填充逻辑
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "update_time", Date.class, new Date());
        RequestTokenBO threadLocalUser = JYangRequestTokenUtil.getThreadLocalUser();
        EmployeeBO employeeBO = threadLocalUser.getEmployeeBO();
        String loginName = employeeBO.getLoginName();
        this.strictUpdateFill(metaObject, "update_man", String.class, loginName);
    }
}
